package com.withebear.introductiontoaerospace;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.io.IOException;

public class MainActivity extends Activity {
    Cursor cursor=null;
    DBOpenHelper myDbHelper=new DBOpenHelper(this);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        try {
            myDbHelper.createDataBase();
        } catch (IOException ioe) {
            throw new Error("Unable to create database");
        }
        try {
            myDbHelper.openDataBase();
        }catch(SQLException sqle){
            throw sqle;
        }

        SQLiteDatabase myDatabase= myDbHelper.getMyDataBase();
        try {
            System.out.println(myDatabase.getPath());
            cursor = myDatabase.rawQuery(" select * from science_questions ", new String[]{});
            cursor.moveToNext();
            final String a = cursor.getString(1);
            System.out.println(a);
            //        cursor=myDatabase.query("art_questions",null,null,null,null,null,null,null);
            //cursor.getColumnIndex("A");
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            final Button button=findViewById(R.id.button1);
            button.setText("Changed");
            button.setOnClickListener(new View.OnClickListener() {
                int i=0;
                public void onClick(View v) {
                    Toast.makeText(MainActivity.this,a,Toast.LENGTH_LONG).show();
                    button.setText("Have been pressed "+i+" times");
                    i++;
                }
            });
            Button button2 =findViewById(R.id.button2);
            button2.setOnClickListener(new View.OnClickListener(){
                public void onClick(View view){
                    Intent intent=new Intent(MainActivity.this,Page2.class);
                    startActivity(intent);
                    finish();
                }
            });
        } catch (Exception exception){
            exception.printStackTrace();
        }finally {
            myDatabase.close();
        }


    }
}